const Mock = require('mockjs')

// 获取table的数据
let List = []
const count = 50
for (let i = 0; i < count; i++) {
    List.push(
        Mock.mock({
            id: Mock.Random.guid(),
            name: Mock.Random.cname(),
            addr: Mock.mock("@county(true)"),
            "age|18-60": 1,
            birth: Mock.Random.date(),
            sex: Mock.Random.integer(0, 1)
        })
    )
}


module.exports = [
    {
        url: '/table/list',
        type: 'get',
        response: config => {
            const { name, current = 1, pageSize = 10 } = config.query
            /**
             * 获取列表
             * 要带参数 name, current, pageSize; name可以不填,
             * current 当前页码数,
             * pageSize 每页的条数
             * @param name,current, pageSize
             * @return {{code: number, count: number, data: *[]}}
             */
            const mockList = List.filter(user => {
                if (name && user.name.indexOf(name) === -1) return false
                return true
            })
            // 分页查询数据
            const pageList = mockList.filter((item, index) => {
                return index < pageSize * current && index >= pageSize * (current - 1)
            })
            console.log('总数：', mockList)
            return {
                code: 200,
                count: mockList.length,
                data: pageList
            }
        }
    },
    {
        url: '/table/add',
        type: 'post',
        response: config => {
            const { id } = config.body
            /**
            * 增加用户
            * @param name, addr, age, birth, sex
            * @return {{code: number, data: {message: string}}}
             */
            const { name, addr, age, birth, sex } = config.body;
            List.unshift({
                id: Mock.Random.guid(),
                name: name,
                addr: addr,
                age: age,
                birth: birth,
                sex: sex
            })
            return {
                code: 200,
                message: "添加成功"
            }
        }
    },
    {
        url: '/table/edit',
        type: 'post',
        response: config => {
            /**
             * 修改用户
             * @param id, name, addr, age, birth, sex
             * @return {{code: number, data: {message: string}}}
             */
            const { id, name, addr, age, birth, sex } = config.body;
            const sex_num = parseInt(sex)
            List.some(u => {
                if (u.id === id) {
                    u.name = name
                    u.addr = addr
                    u.age = age
                    u.birth = birth
                    u.sex = sex_num
                    return true
                }
            })
            return {
                code: 200,
                message: "修改成功"
            }
        }
    },
    {
        url: '/table/del',
        type: 'post',
        response: config => {
            const { id } = config.body
            /**
            * 删除数据
            * @param id
            * @return {*}
             */
            if (!id) {
                return {
                    code: -999,
                    message: "参数不正确"
                }
            } else {
                List = List.filter(u => u.id !== id)
                return {
                    code: 200,
                    message: "删除成功"
                }
            }
        }
    }
]
